/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package BD;

import BE.Condicionpago;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Transaction;
import org.hibernate.Session;

/**
 *
 * @author jcguzmanr
 */
public class CondicionPagoBD {

    private Session session;

    public CondicionPagoBD() {

         this.session = null;

    }

    public int buscaCondicionPedido(int idPedido){
        List lista = null;
        String consulta="";
        consulta="Select srl_idcondicionpago" +
                " From pedido" +
                " where srl_iddocumentotransac="+idPedido;

        Transaction tx =null;
        try {
            session=HibernateUtil.getSessionFactory().getCurrentSession();
            tx = session.beginTransaction();
            SQLQuery sqlquery=session.createSQLQuery(consulta);

            session.flush();
            lista = sqlquery.list();
            tx.rollback();
        } catch (Exception e) {
            e.printStackTrace();
            if(tx!=null)
            {
                tx.rollback();
            }
        }
        int codigo=0;
        try{
            codigo=Integer.parseInt(lista.get(0).toString());
        }
        catch (Exception e)
        { 
            codigo=0;
        }
        return codigo;
    }
    public List<Condicionpago> cargarCondicionPago() {
        List<Condicionpago> lista = null;
        Transaction tx =null;
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            tx = session.beginTransaction();
            String hql = "from Condicionpago order by srlIdcondicionpago";
            Query query = session.createQuery(hql);
            session.flush();
            lista = query.list();
            tx.commit();

        } catch (Exception e) {
            e.printStackTrace();
            if(tx!=null)
            {
                tx.rollback();
            }
        }
        return lista;
    }

}
